package org.mespilus.mespilusweb.datalayer;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.mespilus.mespilusweb.datalayer.usermanagement.Basket;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class CooperativeMode {
	protected static CooperativeMode instance;

	@PrimaryKey
	@Persistent
	protected Key coopId; // "SingleTon" instance

	@Persistent
	protected int numBaskets;

	public CooperativeMode() {
		numBaskets = 5;
		createBaskets(numBaskets);
	}

	public int getNumBaskets() {
		return numBaskets;
	}

	public void setNumBaskets(int numBaskets) {
		this.numBaskets = numBaskets;
	}

	public static CooperativeMode getInstance() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Key key = KeyFactory.createKey(CooperativeMode.class.getSimpleName(),
				"SingleTon");
		instance = pm.getObjectById(CooperativeMode.class, key);
		if (instance == null) {
			instance = new CooperativeMode();
			pm.makePersistent(instance);
		}
		return instance;
	}
	
	private void createBaskets(int numToCreate){
		for(int index=0; index < numToCreate; index++){
			Basket.insert(new Basket());
		}
	}
}
